package gui;

import java.awt.Color;

import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.apache.commons.collections.buffer.*;

import environment.*;

public class MapGUI extends JPanel implements MouseListener {
	private static final long serialVersionUID = 802715225068229758L;

	private final int fieldSize;
	private int startX = 1;
	private int startY = 1;
	private Environment m;
	private LinkedList<Point> pathToDraw;

	public MapGUI(Environment m) {
		addMouseListener(this);
		int height = m.getHeight();
		int width = m.getWidth();
		int less;
		if(height > width)
			less = width;
		else
			less = height;
		
		
		fieldSize = 780/less;
		
		this.m = m;
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		drawLines(g2);
	}

	@Override
	public Dimension getPreferredSize() {
		Dimension size = new Dimension(m.getWidth() * fieldSize, m.getHeight()
				* fieldSize);
		return size;
	}

	private void drawLines(Graphics2D g2) {
		for (int i = 0; i < m.getWidth(); i++) {
			for (int j = 0; j < m.getHeight(); j++) {

				switch (m.getFieldType(i, j)) {
				case WALL:
					g2.setColor(Color.black);
					break;
				case WAY:
					g2.setColor(Color.yellow);
					break;
				case PATH:
					g2.setColor(Color.red);
					break;
				case AGENT:
					g2.setColor(Color.green);
					break;
				}
				drawSingleField(g2, i, j);
			}
		}
		if (pathToDraw != null) {
			for (Point p : pathToDraw) {
				g2.setColor(Color.red);
				drawSingleField(g2, p.x, p.y);
			}
		}

	}

	/**
	 * 
	 * @param g2
	 * @param i
	 * @param j
	 * @
	 */
	private void drawSingleField(Graphics2D g2, int i, int j) {
		g2.draw(new Rectangle2D.Double(i * fieldSize, j * fieldSize, fieldSize,
				fieldSize));
		g2.fillRect(i * fieldSize, j * fieldSize, fieldSize, fieldSize);
	}

	/**
	 * 
	 * @param m
	 * @throws Exception
	 */
	public static void createAndShowGUI(Environment m) throws Exception {
		JFrame frame = new JFrame("czesc");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		MapGUI gui = new MapGUI(m);
		frame.add(gui);
		frame.pack();
		frame.setVisible(true);
	}

	@Override
	public void mouseClicked(final MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(final MouseEvent e) {
		System.out.println(e.getX() / fieldSize + " " + e.getY() / fieldSize);
		try {
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					try {
						long t1 = System.currentTimeMillis();
						pathToDraw = m.getShortestPath(startX, startY, e.getX()
								/ fieldSize, e.getY() / fieldSize);
						double wynik = (double) (System.currentTimeMillis() - t1);
						wynik /= 1000;
						System.out.println("Czas:" + wynik);
						startX = e.getX() / fieldSize;
						startY = e.getY() / fieldSize;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					repaint();
				}
			});

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
